package com.ssyt.wxstore.sys.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ssyt.wxstore.common.BaseDao;
import com.ssyt.wxstore.sys.entity.SysRole;
import com.ssyt.wxstore.sys.entity.SysRoleMenu;

@Repository
public class RoleDao extends BaseDao<SysRole, String> {

	public List<SysRole> getRole() {
		return listEntityByHql("FROM SysRole");
	}

	public int remove(String id) {
		return updateByHql("UPDATE SysRole sr SET sr.state=2 WHERE id=?", id);
	}

	public String add(SysRole role, String[] menuIds) {
		role.setState(1);
		String roleId = save(role);
		for (String menuId : menuIds) {
			SysRoleMenu sysRoleMenu = new SysRoleMenu();
			sysRoleMenu.setSysRoleId(roleId);
			sysRoleMenu.setSysMenuId(menuId);
			getSession().save(sysRoleMenu);
		}
		return roleId;
	}

	public SysRole getUserMenu(String id) {
		return uniqueEntityByHql("FROM SysRole WHERE id=?", id);
	}

	public int update(SysRole role, String[] menuIds) {

		role.setState(1);
		update(role);
		int row = updateByHql("DELETE SysRoleMenu WHERE sysRoleId=?",
				role.getId());
		for (String menuId : menuIds) {
			SysRoleMenu sysRoleMenu = new SysRoleMenu();
			sysRoleMenu.setSysMenuId(menuId);
			sysRoleMenu.setSysRoleId(role.getId());
			getSession().save(sysRoleMenu);
		}
		return row;
	}

}
